package org.osflash.signals;

import asunit.asserts.*;
import org.osflash.signals.events.IEvent;
import org.osflash.signals.support.SpriteWithDeluxeSignals;

class MXMLDeluxeSignalTest {

	var mxmlSprite : SpriteWithDeluxeSignals;
	@:meta(Before())
	public function setUp() : Void {
		mxmlSprite = new SpriteWithDeluxeSignals();
	}

	@:meta(After())
	public function tearDown() : Void {
		mxmlSprite = null;
	}

	@:meta(Test())
	public function mxml_object_has_DeluxeSignals_after_creation() : Void {
		assertTrue(Std.is(mxmlSprite.numChildrenChanged, DeluxeSignal));
		assertTrue(Std.is(mxmlSprite.nameChanged, DeluxeSignal));
		assertTrue(Std.is(mxmlSprite.tabEnabledChanged, DeluxeSignal));
		assertTrue(Std.is(mxmlSprite.tabIndexChanged, DeluxeSignal));
	}

	@:meta(Test())
	public function has_single_value_class_from_mxml_default_property() : Void {
		var valueClasses : Array<Dynamic> = mxmlSprite.numChildrenChanged.valueClasses;
		assertEquals(1, valueClasses.length);
		assertEquals(IEvent, valueClasses[0]);
	}

	@:meta(Test())
	public function has_multiple_value_classes_from_mxml_default_property() : Void {
		var valueClasses : Array<Dynamic> = mxmlSprite.nameChanged.valueClasses;
		assertEquals(2, valueClasses.length);
		assertEquals(String, valueClasses[0]);
		assertEquals(uint, valueClasses[1]);
	}

	@:meta(Test())
	public function has_target_from_mxml_attribute() : Void {
		assertSame(mxmlSprite, mxmlSprite.numChildrenChanged.target);
	}

	@:meta(Test())
	public function has_single_value_class_from_mxml_attribute() : Void {
		var valueClasses : Array<Dynamic> = mxmlSprite.tabEnabledChanged.valueClasses;
		assertEquals(1, valueClasses.length);
		assertEquals(Boolean, valueClasses[0]);
	}

	@:meta(Test())
	public function has_multiple_value_classes_from_mxml_attribute() : Void {
		var valueClasses : Array<Dynamic> = mxmlSprite.tabIndexChanged.valueClasses;
		assertEquals(2, valueClasses.length);
		assertEquals(int, valueClasses[0]);
		assertEquals(Boolean, valueClasses[1]);
	}

	@:meta(Test())
	public function add_listener_then_dispatch_calls_listener() : Void {
		var called : Bool = false;
		var handler : Dynamic = function(newValue : Bool) : Void {
			called = true;
		}
;
		mxmlSprite.tabEnabledChanged.addOnce(handler);
		// when
		mxmlSprite.tabEnabledChanged.dispatch(true);
		// then
		assertTrue(called);
	}

}

